/**
 * 功能:原材料帐 控制器类
 * 开发人员:chengd
 * 创建时间:2012/6/30 16:51:49
 */
package com.towker.frame.controller.finance;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.model.finance.MaterialAcc;
import com.towker.frame.service.finance.MaterialAccService;

//原材料帐
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/finance")
public class MaterialAccController
{
	private MaterialAccService service;

	//业务对象注入
	public void setMaterialAccService(MaterialAccService service)
	{
		this.service = service;
	}

	//添加
	@RequestMapping("/addMaterialAcc{viewName}")
	public ModelAndView addMaterialAcc(HttpServletResponse response, MaterialAcc po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.setViewName("/manage/finance/addMaterialAcc.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.add(po);
				writer.write("添加原材料帐成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加原材料帐失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delMaterialAcc")
	public void delMaterialAcc(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updMaterialAcc{viewName}")
	public ModelAndView updMaterialAcc(HttpServletRequest request, HttpServletResponse response, MaterialAcc po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			result.addObject("po", po);
			result.setViewName("/manage/finance/updMaterialAcc.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.update(po);
				writer.write("修改原材料帐成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改原材料帐失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getMaterialAcc")
	public ModelAndView getMaterialAcc(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		PageModel<MaterialAcc> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/finance/getMaterialAcc.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getMaterialAccById")
	public ModelAndView getMaterialAccById(HttpServletRequest request, HttpServletResponse response)
	{
		MaterialAcc po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.setViewName("/manage/finance/getMaterialAccById.jsp");
		return result;
	}
}
